﻿

namespace Mulala.Cavan.Presentation.Mvc.ViewModel
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Mulala.Cavan.Domain.MainBoundedContext.LogModule;
    using Mulala.Cavan.Presentation.Seedwork;
    using Spring.Context;
    using Spring.Context.Support;
    using Mulala.Cavan.Application.MainBoundedContext.AttachmentModule;
    using Mulala.Cavan.Application.MainBoundedContext.MulalaModule;
    using Mulala.Cavan.Domain.MainBoundedContext.SecurityModule;
    using Mulala.Cavan.Domain.MainBoundedContext.NannyModule;
    using Mulala.Cavan.Domain.MainBoundedContext.ProprietorModule;
    using Mulala.Cavan.Domain.MainBoundedContext.CorporationModule;

    public class VisitHistoryViewModelConverter
    {
        public static IList<IVisitHistoryViewModel> Convert(IList<VisitHistory> visitHistories)
        {
            IApplicationContext springContext = ContextRegistry.GetContext();
            IAttachmentAppService attachmentAppService = springContext.GetObject(MulalaAppServiceConst.ATTACHMENTAPPSERVICE) as IAttachmentAppService;

            IList<IVisitHistoryViewModel> visitors = new List<IVisitHistoryViewModel>();
            foreach (VisitHistory vh in visitHistories)
            {
                IVisitHistoryViewModel visitHistoryViewModel = ModelFactory.CreateModel<IVisitHistoryViewModel>();
                visitHistoryViewModel.Item = vh;
                if ((SecurityIdentifier.Role)vh.Visitor.Roles[0].ID == SecurityIdentifier.Role.Nanny)
                {
                    visitHistoryViewModel.VisitorAvatar = attachmentAppService.GetNannyAvatar(NannyFactory.CreateNanny(vh.Visitor.ID));
                }
                if ((SecurityIdentifier.Role)vh.Visitor.Roles[0].ID == SecurityIdentifier.Role.Proprietor)
                {
                    visitHistoryViewModel.VisitorAvatar = attachmentAppService.GetProprietorAvatar(ProprietorFactory.CreateProprietor(vh.Visitor.ID));
                }
                if ((SecurityIdentifier.Role)vh.Visitor.Roles[0].ID == SecurityIdentifier.Role.Corporation)
                {
                    visitHistoryViewModel.VisitorLogo = attachmentAppService.GetCorporationLogo(CorporationFactory.CreateCorporation(vh.Visitor.ID));
                }
                visitors.Add(visitHistoryViewModel);
            }

            return visitors;
        }
    }
}
